/**
 * @author Kevin Gomes
 */

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.sql.*;

import utils.*; //utils written for this project. Has MysqlConnect.java

/**
 * Class: PClient
 *
 * Each Peer acts as a Client as well as Server. This class handles the Client role of a Peer.
 * This class would be used by the Peer class.
 * In addition, this class includes the main() method that demonstrates how this class can be used.
 */
public class PClient {
    /**
     * Peer IP Address.
     */
    private String peer_ip;

    /**
     * Peer name.
     */
    private String peer_name;

    /**
     * DB Connection.
     */
    private MysqlConnect conn;

    /**
     * Constructor.
     */
    public PClient() { }

    public void init(String peer_name) {
	//Save peer ip address
	this.setPeerIp(this.getIp());

	//Save peer name
	this.setPeerName(peer_name);

	//Setup a database connection
        this.conn = new MysqlConnect();

	//save file list to the database.
        this.updateAvailableFiles();
    }

    /**
     * Getters and Setters
     */
    public void setPeerName(String value) { this.peer_name = value; }
    public String getPeerName() { return this.peer_name; } 
    public void setPeerIp(String value) { this.peer_ip = value; }
    public String getPeerIp(String value) { return this.peer_ip; }

    /**
     * Returns the IP Address of the machine this client is running on.
     */
    public String getIp(){
        String ip=new String ();
        try{
            InetAddress ownIP = InetAddress.getLocalHost();
            ip = ownIP.getHostAddress();
        }catch (Exception e){
            System.out.println("Exception caught ="+e.getMessage());
        }
        return ip;
    }

    /**
     * Updates the list of available files with the peer.
     * The list of available files are stored in mysql databases.
     * Ideally, each peer would have a local instance of its DB.
     * But, in this case all peers use a shared database.
     */
    public void updateAvailableFiles() {
	ArrayList files = this.getFilesInDir();
	//System.out.println("All Files: " + files.toString());

	try {
	    this.conn.insertInPeerFiles(this.peer_ip, this.peer_name, files);
	} catch(Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * Returns an array of files on the Client.
     */
    public ArrayList getFilesInDir() {
        ArrayList files = new ArrayList();

        try {
            File dir = new File("./");
            String[] children = dir.list();

            for(int i = 0;i < children.length;i++) {
                files.add(children[i]);
            }
        } catch (Exception e) {
            System.out.println(e);
        }

        return files;
    }

    /**
     * Once the Client is instantiated all initial activities are taken care by the contructor.
     * The main() method demonstrates how this class is to be used.
     */
    public static void main(String args[]) throws Exception {
	if(args.length != 1) {
	    System.out.println("Syntax: java PClient <peer_name>");
	    System.exit(1);
	}

	PClient obj = new PClient();
	obj.init(args[0]);
    }
}